package com.bss.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.Service;
import com.bss.model.Bed;
import com.bss.model.Building;
import com.bss.model.Room;
import com.bss.service.BedService;
import com.bss.service.BuildingService;
import com.bss.service.RoomService;

@Controller
@Scope("request")
public class BedController extends com.andy.basics.Controller<Bed> {

	private Building building = new Building();

	@Autowired
	private RoomService roomService;

	@Autowired
	private BedService bedService;

	@Override
	public Service<Bed> getService() {
		return this.bedService;
	}

	public void setBedService(final BedService bedService) {
		this.bedService = bedService;
	}

	@Override
	public Bed create() {
		return new Bed();
	}

	@Override
	public String getModelName() {
		return "Bed";
	}

	@Override
	public String select() {
		super.select();
		this.building = this.getModel().getRoom().getBuilding();
		return null;
	}

	public List<Room> getRooms() {
		if (this.building == null) {
			return null;
		}

		return this.roomService.findDormitories(this.building.getName());

	}

	public void setBuilding(final Building building) {
		this.building = building;
	}

	public Building getBuilding() {
		return this.building;
	}

	public void selectBuilding() {

	}

	@Override
	public String add() {
		try {
			this.setError(false);
			this.getErrors().clear();
			return super.add();
		} catch (Exception e) {
			this.getMessageHandler().addMessage("AddError", e.getMessage());
			this.setError(true);
			this.getErrors().add(e.getMessage());
		}

		return null;
	}

}
